/**
 * Copyright (c) 2006 RiseSoft Co.,Ltd  
 * $Header$
 */
package cn.newtest.bi.collection.model;

import java.io.Serializable;
import java.util.Iterator;
import java.util.List;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;

/**
 * 统计对象层次结构定义
 * 
 * @author <a href="mailto: chenjian@risesoft.net">陈健</a>
 * @version $Revision$
 * 
 */
@Entity
@org.hibernate.annotations.Entity(dynamicInsert=true,dynamicUpdate=true)
@Table(name="Rpt_StatisticalObjectTree")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
public class StatisticalObjectTree implements Comparable<Object>, Serializable {

	private static final long serialVersionUID = 6771922723075604306L;

	@Id
	@Column(name="SOT_GUID",length=38)
	@GenericGenerator(name="StatisticalObjectTree-assigned",strategy="assigned")
	private String guid;

	@Column(name="SOT_Name",length=100)
	private String name;

	@Column(name="isEnabled")
	private Boolean enabled;

	@Column(name="SOT_Type")
	private Integer type;

	@Column(name="SOT_Description",length=200)
	private String description;

	@Column(name="SOT_IconPath",length=100)
	private String iconPath;

	@Basic
	private Double tabIndex;

	@OneToMany(cascade=CascadeType.REMOVE,fetch=FetchType.LAZY,mappedBy="statisticalObjectTree")
	@OrderBy("tabIndex")
	private List<StatisticalObjectTreeNode> sotNodes;

	@ManyToOne(fetch=FetchType.LAZY)
	@JoinColumn(name="App_GUID",nullable=true)
	private Application application;

	@Transient
	public String getAppGuid() {
		if (application != null) {
			return application.getGuid();
		}
		return "";
	}

	public void setAppGuid(String appGuid) {
		if (appGuid != null && appGuid.trim().length() > 0) {
			if (application == null) {
				application = new Application();
			}
			application.setGuid(appGuid);
		}
	}

	@Transient
	public StatisticalObjectTreeNode getRootNode() {
		StatisticalObjectTreeNode rootNode = null;
		if (sotNodes != null) {
			Iterator<StatisticalObjectTreeNode> it = sotNodes.iterator();
			while (it.hasNext()) {
				StatisticalObjectTreeNode node = (StatisticalObjectTreeNode)it.next();
				if (StringUtils.isBlank(node.getParentColumnName())
						&& StringUtils.isBlank(node.getSelfColumnName())) {
					rootNode = node;
					break;
				}
			}
		}
		return rootNode;
	}

	@Transient
	public StatisticalObjectTreeNode getNodeByCatalog(String catalogGuid) {
		StatisticalObjectTreeNode node = null;
		if (sotNodes != null) {
			for (int i = 0; i < sotNodes.size(); i++) {
				StatisticalObjectTreeNode sotNode = (StatisticalObjectTreeNode) sotNodes.get(i);
				Catalog catalog = sotNode.getCatalog();
				if (catalog.getGuid().equals(catalogGuid)) {
					node = sotNode;
					break;
				}
			}
		}
		return node;
	}
	
	/**
	 * 根据 sotNodeGuid 来判断处于树中的第几层，从 0 层开始
	 */
	@Transient
	public int getScopeNumber(String sotNodeGuid) {
		StatisticalObjectTreeNode rootSotNode = this.getRootNode();
		return this.recursiveJudgeScopeNumber(rootSotNode, sotNodeGuid, 0);
	}
	
	private int recursiveJudgeScopeNumber(StatisticalObjectTreeNode sotNode,String sotNodeGuid,int scopeNumber) {
		if(sotNode.getGuid().equals(sotNodeGuid))
			return scopeNumber;
		else{
			scopeNumber ++;
			List<StatisticalObjectTreeNode> children = sotNode.getChildren();
			for(StatisticalObjectTreeNode s:children){
				int rvalue = this.recursiveJudgeScopeNumber(s, sotNodeGuid, scopeNumber);
				if(rvalue>0)
					return rvalue;
			}
		}
		return -1;
	}
	
	/**
	 * 根据sotNodeGuid获得结点
	 * 
	 * @param sotNodeGuid
	 * @return
	 */
	@Transient
	public StatisticalObjectTreeNode getSotNodeBySotNodeGuid(String sotNodeGuid) {
		StatisticalObjectTreeNode rootSotNode = this.getRootNode();
		if(rootSotNode.getGuid().equals(sotNodeGuid))
			return rootSotNode;
		List<StatisticalObjectTreeNode> offspring = rootSotNode.getOffspring();
		for(StatisticalObjectTreeNode sotNode:offspring){
			if(sotNode.getGuid().equals(sotNodeGuid)){
				return sotNode;
			}
		}
		return null;
	}
	
	public Application getApplication() {
		return application;
	}

	public void setApplication(Application application) {
		this.application = application;
	}

	public List<StatisticalObjectTreeNode> getSotNodes() {
		return sotNodes;
	}

	public void setSotNodes(List<StatisticalObjectTreeNode> sotNodes) {
		this.sotNodes = sotNodes;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getGuid() {
		return guid;
	}

	public void setGuid(String guid) {
		this.guid = guid;
	}

	public String getIconPath() {
		return iconPath;
	}

	public void setIconPath(String iconPath) {
		this.iconPath = iconPath;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Double getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(Double tabIndex) {
		this.tabIndex = tabIndex;
	}

	public Integer getType() {
		return type;
	}

	public void setType(Integer type) {
		this.type = type;
	}

	/**
	 * @see java.lang.Comparable#compareTo(Object)
	 */
	public int compareTo(Object object) {
		StatisticalObjectTree myClass = (StatisticalObjectTree) object;
		return new CompareToBuilder().append(this.tabIndex, myClass.tabIndex)
				.append(this.type, myClass.type).append(this.enabled,
						myClass.enabled).append(this.description,
						myClass.description).append(this.iconPath,
						myClass.iconPath).append(this.guid, myClass.guid)
				.append(this.name, myClass.name).toComparison();
	}

	/**
	 * @see java.lang.Object#equals(Object)
	 */
	public boolean equals(Object object) {
		if (!(object instanceof StatisticalObjectTree)) {
			return false;
		}
		StatisticalObjectTree rhs = (StatisticalObjectTree) object;
		return new EqualsBuilder().appendSuper(super.equals(object)).append(
				this.tabIndex, rhs.tabIndex).append(this.type, rhs.type)
				.append(this.enabled, rhs.enabled).append(this.description,
						rhs.description).append(this.iconPath, rhs.iconPath)
				.append(this.guid, rhs.guid).append(this.name, rhs.name)
				.isEquals();
	}

	/**
	 * @see java.lang.Object#hashCode()
	 */
	public int hashCode() {
		return new HashCodeBuilder(-593945373, 1100105203).appendSuper(
				super.hashCode()).append(this.tabIndex).append(this.type)
				.append(this.enabled).append(this.description).append(
						this.iconPath).append(this.guid).append(this.name)
				.toHashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return new ToStringBuilder(this).append("tabindex", this.tabIndex)
				.append("name", this.name).append("description",
						this.description).append("iconPath", this.iconPath)
				.append("enabled", this.enabled).append("type", this.type)
				.append("guid", this.guid).toString();
	}
}